/**
 * Created by hp on 2017/7/9.
 */
function initDatabase() {//初始化数据库
    var db = getCurrentDb();
    if(!db) {
        alert("您的浏览器不支持HTML5");
        return;
    }


    db.transaction(function (trans) {
        trans.executeSql("create table if not exists Register(userName text null,userEmail text null,userPassword text null)", [], function (trans, result) {

        }, function (trans, message) {
            alert(message);
        });
    }, function (trans, result) {
    }, function (trans, message) {
    });
}

$(function () {

    initDatabase();

    $("#btnSubmit").click(function () {

        var returnValue = true;
        //姓名不少于两个字
        var username = frmRegister.Name.value;
        if(username.length < 2) {
            returnValue = false;
            alert("姓名不少于两个字。");
            frmRegister.Name.value = "";
            frmRegister.Name.focus();
            return false;
            //return false 才能不刷新页面
        }

        //密码
        var password1 = frmRegister.Password.value;
        if(password1.length < 6) {
            alert("密码不少于6位。");
            frmRegister.Password.value = "";
            frmRegister.PasswordAgain.value = "";
            frmRegister.Password.focus();
            returnValue = false;
            return false;
        }




        //再次输入密码
        var password2 = frmRegister.PasswordAgain.value;
        if(password2 != password1){
            alert("密码不匹配，请再次输入密码。");
            frmRegister.PasswordAgain.value = "";
            frmRegister.PasswordAgain.focus();
            returnValue = false;
            return false;
        }


        var txtName = $("#Name").val();
        var txtEmail = $("#Email").val();
        var txtPassword = $("#Password").val();

        var db = getCurrentDb();


        db.transaction(function (trans) {
            trans.executeSql("select * from Register where  userName=?", [txtName], function (ts, data) {
                if (data) {
                    for (var i = 0; i < data.rows.length; i++) {
                        var row =data.rows.item(i);//
                        if(row.userPassword){
                            alert("该用户名已被注册，请重新注册！");
                            window.location.href="register.html";
                            return;
                        }
                    }
                    trans.executeSql("insert into Register(userName,userEmail,userPassword) values(?,?,?) ", [txtName,txtEmail, txtPassword], function (ts, data) {
                    }, function (ts, message) {
                        alert(message);
                    });
                    alert("提交成功!去登陆。");
                    window.location.href="login.html";
                }

            }, function (ts, message) {
                alert("true");
                alert(message);
                var tst = message;
            });


        });
    });
});

function getCurrentDb() {
    //打开数据库，或者直接连接数据库参数：数据库名称，版本，概述，大小
    var db = openDatabase("myDb", "1.0", "it's to save register data!", 1024 * 1024); ;
    return db;

}

function passStrength(form) {
    //密码
    var password1 = frmRegister.Password.value;
    var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
    var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
    if(password1.length >= 6) {
        if (strongRegex.test(password1))
        {
            var ps=document.getElementById("passstrength1");
            ps.innerHTML='密码强度：强!';
        }
        else if (mediumRegex.test(password1))
        {
            var ps=document.getElementById("passstrength1");
            ps.innerHTML='密码强度：中!';
        }
        else
        {
            var ps=document.getElementById("passstrength1");
            ps.innerHTML='密码强度：弱!';

        }
    }
}
